/**
 * kaGridCols 封装
 * 
 * 版本：v1.0
 * 
 * Stanley
 **/
layui.define(['jquery'],
    function (exports) {
        var $ = layui.jquery;
        var kaGridCols = new function () {
            this.get = function (params) {
                var kaGridAllColsName = "table_Cols_" + params.id;
                // 判断是非更新
                if (params.colsBoolean == 0) { // 单纯获取本地
                    return [top.kaGridAllCols[kaGridAllColsName]];
                } else if (params.colsBoolean == 3) { // 单纯获取本地
                    // 初始化
                    var cols = [];
                    cols.push({ type: 'numbers', title: `<i class="icon Kaultraiconfont Kaultraicon-K" style="font-weight: 900;color: red;"></i>`, width: 40, fixed: 'left' });
                    if (params.checkboxBoolean) {
                        cols.push({ type: 'checkbox', fixed: 'left' });
                    }
                    Array.prototype.push.apply(cols, params.cols);
                    if (top.kaGridAllCols == undefined || top.kaGridAllCols == null) { top.kaGridAllCols = {}; }
                    top.kaGridAllCols[kaGridAllColsName] = cols;
                    // 判断字段是否加载模版
                    for (var i in cols) {
                        var col = cols[i];
                        if(col.templetType != undefined && col.templetType != null && col.templetType != ""){
                            col.templet = layui.kaGridColsTemplet.init(params, col.templetType);
                        }
                    }
                    return [cols];
                } else {
                    // 加载后端数据
                    var cols = [];
                    // 加载 序号 和 复选框/单选框
                    cols.push({ type: 'numbers', title: `<i class="icon Kaultraiconfont Kaultraicon-K" style="font-weight: 900;color: red;"></i>`, width: 40, fixed: 'left' });
                    if (params.checkboxBoolean) {
                        cols.push({ type: 'checkbox', fixed: 'left' });
                    }
                    if (params.colsBoolean == 1) { // 初始化
                        // 请求后端数据
                        layui.kaAjax.ajax(layui.kaBgUrl.url(params.cols), function (rs) {
                            if (rs.code == 200) {
                                params.cols = rs.data;
                                // 修复 with 不正确显示
                                for (var j in params.cols) {
                                    var colsx =  params.cols[j];
                                    if (colsx.width == null) {
                                       delete colsx.width
                                    } else {
                                        colsx.width = parseInt(colsx.width);
                                    }
                                    if (colsx.minWidth == null) {
                                       delete colsx.minWidth
                                    } else {
                                        colsx.width = parseInt(colsx.minWidth);
                                    }
                                    if (colsx.maxWidth == null) {
                                       delete colsx.maxWidth
                                    } else {
                                        colsx.width = parseInt(colsx.maxWidth);
                                    }
                                    if (colsx.expandedWidth == null) {
                                       delete colsx.expandedWidth
                                    } else {
                                        colsx.expandedWidth = parseInt(colsx.expandedWidth);
                                    }
                                    if (colsx.align == null) {
                                       delete colsx.align
                                    }
                                    if (colsx.colspan == null) {
                                       delete colsx.colspan
                                    }
                                    if (colsx.createBy == null) {
                                       delete colsx.createBy
                                    }
                                    if (colsx.createTime == null) {
                                       delete colsx.createTime
                                    }
                                    if (colsx.createTimeKaJudge == null) {
                                       delete colsx.createTimeKaJudge
                                    }
                                    if (colsx.edit == null) {
                                       delete colsx.edit
                                    }
                                    if (colsx.escapeNumber == null) {
                                       delete colsx.escapeNumber
                                    }
                                    if (colsx.event == null) {
                                       delete colsx.event
                                    }
                                    if (colsx.expandedMode == null) {
                                       delete colsx.expandedMode
                                    }
                                    if (colsx.exportTemplet == null) {
                                       delete colsx.exportTemplet
                                    }
                                    if (colsx.fieldTitle == null) {
                                       delete colsx.fieldTitle
                                    }
                                    if (colsx.fixed == null) {
                                       delete colsx.fixed
                                    }
                                    if (colsx.hide == null) {
                                       delete colsx.hide
                                    }
                                    if (colsx.ignoreExport == null) {
                                       delete colsx.ignoreExport
                                    }
                                    if (colsx.ignoreExportNumber == null) {
                                       delete colsx.ignoreExportNumber
                                    }
                                    if (colsx.remark == null) {
                                       delete colsx.remark
                                    }
                                    if (colsx.rowspan == null) {
                                       delete colsx.rowspan
                                    }
                                    if (colsx.sort == null) {
                                       delete colsx.sort
                                    }
                                    if (colsx.sortNumber == null) {
                                       delete colsx.sortNumber
                                    }
                                    if (colsx.style == null) {
                                       delete colsx.style
                                    }
                                    if (colsx.templetType == null) {
                                       delete colsx.templetType
                                    }
                                    if (colsx.totalRow == null) {
                                       delete colsx.totalRow
                                    }
                                    if (colsx.unresize == null) {
                                       delete colsx.unresize
                                    }
                                    if (colsx.createBy == null || colsx.createBy == "") {
                                       delete colsx.createBy
                                    }
                                    if (colsx.createBy == null || colsx.createBy == "") {
                                       delete colsx.createBy
                                    }
                                    if (colsx.updateBy == null || colsx.updateBy == "") {
                                       delete colsx.updateBy
                                    }
                                    if (colsx.updateTime == null) {
                                       delete colsx.updateTime
                                    }
                                    delete colsx.hideNumber;
                                    delete colsx.kasort;
                                }
                            }
                        }, {id: params.bodyUrlId});
                        Array.prototype.push.apply(cols, params.cols);
                        // 初始化
                        if (top.kaGridAllCols == undefined || top.kaGridAllCols == null) { top.kaGridAllCols = {}; }
                        top.kaGridAllCols[kaGridAllColsName] = cols;
                        for (var i in cols) {
                            var col = cols[i];
                            if(col.templetType != undefined && col.templetType != null && col.templetType != ""){
                                col.templet = layui.kaGridColsTemplet.init(params, col.templetType);
                            }
                        }
                        // 赋值
                        params.cols = [cols];
                        // 返回
                        return params.cols;
                    } else if (params.colsBoolean == 2) { // 更新
                        params.colsBoolean = 1;
                        params.cols = layui.kaGridCols.get(params);
                        // 更新表格
                        layui.kaGrid.render(params);
                    }
                }
            }
        };
        exports('kaGridCols', kaGridCols);
    }
)